"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : PACKAGE
Case Name   : 触发器中调用package,合理报错
Create At   : 2022/04/27
Owner       : @zou_jialiang050
Description :
    1.建表并插入数据
    2.声明包规格
    3.定义包体
    4.创建触发器,调用package中的函数
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.合理报错
    5.成功
History     :
"""

import os
import unittest
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class PACKAGE(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.package = 'p_test_0062'
        self.table = 't_test_0062'
        self.trigger = 't_package_test_0062'
        self.function = 'f_test_0062'

    def test_package(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = 'step1:建表并插入数据 expect:成功'
        self.logger.info(step)
        create_table = self.primary_sh.execut_db_sql(f'''
            drop table if exists {self.table};
            create table {self.table}(core int,name varchar(10));
            insert into {self.table} values(50,'test'),(100,'tom'),
            (120,'john'),(40,'kitty');''')
        self.logger.info(create_table)
        self.assertIn(self.Con.TABLE_DROP_SUCCESS, create_table,
                      '执行失败' + step)
        self.assertIn(self.Con.CREATE_TABLE_SUCCESS, create_table,
                      '执行失败' + step)
        self.assertIn(self.Con.INSERT_SUCCESS_MSG, create_table,
                      '执行失败' + step)

        step = 'step2:声明包规格 expect:成功'
        self.logger.info(step)
        spec_package = self.primary_sh.execut_db_sql(
            f'''create or replace package {self.package} is 
            function {self.function} return varchar;
            end {self.package};''')
        self.logger.info(spec_package)
        self.assertIn(self.Con.create_package_success_msg, spec_package,
                      '执行失败' + step)

        step = 'step3:定义包体 expect:成功'
        self.logger.info(step)
        def_package = self.primary_sh.execut_db_sql(
            f'''create or replace package body {self.package} is 
            function {self.function} return varchar is 
            declare
                v_sql varchar(2000);
            begin
                v_sql := (select name from {self.table} where core = 100);
                return v_sql;
            end;
            end {self.package};''')
        self.logger.info(def_package)
        self.assertIn(self.Con.create_package_body_success_msg, def_package,
                      '执行失败' + step)

        step = 'step4:创建触发器,调用package中的函数 expect:合理报错'
        self.logger.info(step)
        create_trigger = self.primary_sh.execut_db_sql(
            f'''create trigger {self.trigger} after insert on {self.table} for 
            each row execute procedure {self.package}.{self.function}();''')
        self.logger.info(create_trigger)
        self.assertIn(f'ERROR:  function {self.package}.{self.function} must '
                      f'return type "trigger"', create_trigger, '执行失败' +
                      step)

    def tearDown(self):
        step = 'step5:清理环境 expect:清理环境成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop table {self.table};
            drop package {self.package};''')
        self.logger.info(clean_environment)
        self.assertIn(self.Con.TABLE_DROP_SUCCESS, clean_environment,
                      '执行失败' + step)
        self.assertIn(self.Con.drop_package_success_msg, clean_environment,
                      '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
